package com.cyandpos.pad.pay.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.cyandpos.pad.R;
import com.cyandpos.pad.pay.listenter.MyClickListener;
import com.cyandpos.pad.posdata.CardCouponItem;
import com.cyandpos.pad.utils.MoneyUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by zx on 2017/3/22.
 */

public class CardCouponPayAdapter extends BaseAdapter {
    private Context mContext;
    public List<CardCouponItem> list = new ArrayList<>();
    private MyClickListener mListener;
    private UseOnClickListener useOnClickListener;
    public CardCouponPayAdapter(Context mContext, List<CardCouponItem> list, MyClickListener listener) {
        this.mContext = mContext;
        this.list = list;
        mListener = listener;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public CardCouponItem getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder ;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.yhq_lv_item, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.yhqName.setText(getItem(position).getCouponTypeName());
        holder.yhqVipId.setText(getItem(position).getCardId()+"");
        int canUseMoney = getItem(position).getBalance() < getItem(position).getLimitMoney()? getItem(position).getBalance():getItem(position).getLimitMoney();
        holder.availableAmount.setText(MoneyUtils.getMoneyStr(canUseMoney));//允许金额
        holder.yhqBalance.setText(MoneyUtils.getMoneyStr(getItem(position).getBalance()));//余额
        holder.yhqUsedMoney.setText(MoneyUtils.getMoneyStr(getItem(position).getUsedMoney()));//使用金额
        holder.btnAllUse.setOnClickListener(mListener);
        holder.btnAllUse.setTag(position);
        holder.btnUse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(useOnClickListener!=null){
                    useOnClickListener.onClickListener(position);
                }
            }
        });
        if (position % 2 == 0) {
            holder.llCardCoupon.setBackgroundResource(R.mipmap.yhq_even);
        } else {
            holder.llCardCoupon.setBackgroundResource(R.mipmap.yhq_odd);
        }
        return convertView;
    }

    public static class ViewHolder{
        @BindView(R.id.yhqItem_tvName)
        TextView yhqName;
        @BindView(R.id.yhqItem_tvVipId)
        TextView yhqVipId;
        @BindView(R.id.yhqItem_availableAmount)
        TextView availableAmount;
        @BindView(R.id.yhqItem_balance)
        TextView yhqBalance;
        @BindView(R.id.yhqItem_usedMoney)
        TextView yhqUsedMoney;
        @BindView(R.id.yhqItem_bt_allUse)
        Button btnAllUse;
        @BindView(R.id.yhqItem_bt_Use)
        Button btnUse;
        @BindView(R.id.ll_cardCoupon)
        LinearLayout llCardCoupon;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

    }

    public interface  UseOnClickListener{
        void onClickListener(int position);
    }

    public void setUseOnClickListener(UseOnClickListener useOnClickListener) {
        this.useOnClickListener = useOnClickListener;
    }
}
